package com.wxapp.common.util.feign.impl;
import com.wxapp.common.constant.pojo.vm.ResultVm;
import com.wxapp.common.constant.pojo.vo.FeignRequestVo;
import com.wxapp.common.util.feign.DynamicFeignUtil;
import com.wxapp.common.util.feign.FeignClientFactoryService;
import com.wxapp.common.util.feign.feign.DynamicFeign;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

@Component
@RequiredArgsConstructor
public class DynamicFeignUtilImpl implements DynamicFeignUtil {
    private final FeignClientFactoryService<DynamicFeign> feignFeignClientFactory;

    @Override
    public <T> ResultVm<Object> executeApi(FeignRequestVo<T> feignRequest) {
        ResultVm<Object> result = new ResultVm<>();
        DynamicFeign dynamicService = feignFeignClientFactory.getFeignClient(DynamicFeign.class, feignRequest.getSysNameEnum().getValue());
        switch (feignRequest.getHttpMethod().name()) {
            case "POST" ->
                    result = dynamicService.executePostApi(feignRequest.getUrl(), feignRequest.getToken(), feignRequest.getParams());
            case "GET" ->
                    result = dynamicService.executeGetApi(feignRequest.getUrl(), feignRequest.getToken(), feignRequest.getParams());
            case "DELETE" ->
                    result = dynamicService.executeDeleteApi(feignRequest.getUrl(), feignRequest.getToken(), feignRequest.getParams());
            case "PUT" ->
                    result = dynamicService.executePutApi(feignRequest.getUrl(), feignRequest.getToken(), feignRequest.getParams());
        }
        return result;
    }
}
